package hfuu.com.cn.teasystemapp;

import android.content.Intent;
import android.os.Looper;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.ImageView;
import android.widget.Toast;

import com.google.gson.Gson;

import java.io.IOException;
import java.util.regex.Pattern;

import hfuu.com.cn.teasystemapp.bean.operatern;
import hfuu.com.cn.teasystemapp.util.HttpUtils;
import okhttp3.Call;
import okhttp3.Callback;
import okhttp3.Response;

public class rnadd extends AppCompatActivity {

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_rnadd);

        Button ok= (Button) findViewById(R.id.ok);
        Button cancel= (Button) findViewById(R.id.cancel);


        final EditText time= (EditText) findViewById(R.id.time);

        ImageView back = (ImageView) findViewById(R.id.back);

        Intent i=getIntent();
        final String id=i.getStringExtra("id");
        final String teaxid=i.getStringExtra("teaxid");


        back.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                finish();
            }
        });
        cancel.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                finish();
            }
        });

        ok.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                String time1=time.getText().toString().trim();
                if (time1.length()==0){
                    Toast.makeText(rnadd.this,"请输入内容",Toast.LENGTH_SHORT).show();
                    return;
                }
                Pattern pattern=Pattern.compile("^((0)|([1-9][0-9]*))\\.[0-9]+$");
                Pattern p = Pattern.compile("^[-\\+]?[\\d]*$");
                if(!(pattern.matcher(time1).matches()||p.matcher(time1).matches())){
                    Toast.makeText(rnadd.this,"输入数字",Toast.LENGTH_SHORT).show();
                    return;
                }
                operatern rn=new operatern();
                rn.setOperateTotaltime(time1);
                rn.setOperateId(id);
                rn.setOperateTeaxid(teaxid);
                Gson gson=new Gson();
                String pickjson= gson.toJson(rn);

                HttpUtils.doPost("operate/rnprocad", pickjson, new Callback() {
                    @Override
                    public void onFailure(Call call, IOException e) {
                        Looper.prepare();
                        Toast.makeText(rnadd.this, "网络连接失败", Toast.LENGTH_LONG).show();
                        Looper.loop();
                    }

                    @Override
                    public void onResponse(Call call, Response response) throws IOException {
                        String res=response.body().string();
                        if(res.equals("success")){
                            Looper.prepare();
                            Toast.makeText(rnadd.this, "添加成功", Toast.LENGTH_LONG).show();
                            Looper.loop();

                            rnadd.this.finish();
                            return;
                        }else{
                            Looper.prepare();
                            Toast.makeText(rnadd.this, "添加失败", Toast.LENGTH_LONG).show();
                            Looper.loop();
                        }
                    }
                });
                finish();
            }
        });

    }
}
